<template>
  <div>
    <el-table
      :data="eventsForUserList"
      style="width: 100%"
      stripe
      border
      header-row-class-name="base-table-header-v3"
      row-class-name="base-table-row-v3"
      v-loading="loading"
    >
      <el-table-column :label="$t('common.label.eventNumber')" sortable prop="eventNo" min-width="150">
        <template slot-scope="scope">
          <a 
            href="#" 
            @click="openDetail(scope.row, `/event/view/${scope.row.id}`)" 
            class="task-link">
            {{scope.row.eventNo}}
          </a>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('common.event.eventType')"
        prop="templateName"
      ></el-table-column>
      <el-table-column :label="$t('common.label.customerName')" prop="cusName"></el-table-column>

      <el-table-column :label="$t('common.label.status')" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 'created'"> {{ $t('common.event.stateProcess.created') }} </span>
          <span v-else-if="scope.row.state == 'allocated'"> {{ $t('common.event.stateProcess.allocated') }} </span>
          <span v-else-if="scope.row.state == 'processing'"> {{ $t('common.event.stateProcess.processing') }} </span>
          <span v-else-if="scope.row.state == 'finished'"> {{ $t('common.event.stateProcess.finished') }} </span>
          <span v-else-if="scope.row.state == 'offed'"> {{ $t('common.event.stateProcess.offed') }} </span>
          <span v-else-if="scope.row.state == 'convert2Task'"> {{ $t('common.event.stateProcess.convert2Task') }} </span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('common.label.createTime')" sortable prop="createTime">
        <template slot-scope="scope">
          {{ scope.row.createTime | fmt_datetime }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.label.finishTime')" sortable prop="completeTime">
        <template slot-scope="scope">
          {{ scope.row.completeTime | fmt_datetime }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="base-pagination"
      :background="true"
      layout="total, prev, pager, next"
      :total="total"
      :page-size="pageInfo.pageSize"
      :current-page="pageInfo.pageNum"
      @current-change="jump"
    >
    </el-pagination>
  </div>
</template>

<script>
import * as DeptHomeApi from "@src/api/DeptHomeApi";
import Platform from '@src/platform'
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
import { openAccurateTab } from '@src/util/platform'
export default {
  name: "dept-events-list",
  props: {
    detailId: {
      type: String,
      default: "",
    },
    isFromUserView: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      eventsForUserList: [], // 成员列表
      pageInfo: {
        // 分页
        pageNum: 1,
        pageSize: 10,
      },
      total: 0, // 总数
      loading: false
    };
  },
  mounted() {
    this.getDeptEventsForUserView();
  },
  methods: {
    // 部门主页服务事件
    getDeptEventsForUserView() {
      this.loading = true;
      const params = { ...this.pageInfo };
      if(this.isFromUserView) {
        params.executor = this.detailId;
      } else {
        params.tagId = this.detailId;
      }
      DeptHomeApi.getDeptEventsForUserView(params).then((res) => {
        this.eventsForUserList = res.list;
        this.total = res.total;

        this.loading = false;
      }).catch(e=>{
        this.loading = false;
        console.error('fetch task caught e', e)
      })
    },
    jump(e) {
      this.pageInfo.pageNum = e;
      this.getDeptEventsForUserView();
    },
    // 跳转工单详情
    openDetail(row, url){
      if (!window.frameElement) return;

      let fromId = window.frameElement.getAttribute('id');
      
      //TODO: 统一id命名
      // Platform.openTab({
      //   id: "eventView_" + row.id,
      //   title: `事件${row.eventNo}`,
      //   close: true,
      //   url,
      //   fromId
      // });
      openAccurateTab({
        type: PageRoutesTypeEnum.PageEventView,
        key: row.id,
        titleKey: row.eventNo,
        fromId
      })
    },
  },
};
</script>

<style lang="scss" scoped>
.task-link {
  @include fontColor();
}
</style>